<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:logger="http://www.matasek.net/DatabaseLogger"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="logger xsi">

    <xsl:output method="html" encoding="UTF-8" indent="yes"
        doctype-public="-//W3C//DTD HTML 4.01//EN"
        doctype-system="http://www.w3.org/TR/html4/strict.dtd"/>

    <xsl:template match="/logger:query">
        <html>
            <head>
                <title>Logging table</title>
                <!-- style inspired by PgAdmin application http://www.pgadmin.org/ -->
                <style type="text/css">
                    body {
                        font-family: verdana, helvetica, sans-serif;
                        margin: 0;
                        padding: 0;
                    }
                    h1 {
                        font-weight: bold;
                        font-size: 150%;
                        border-bottom-style: solid;
                        border-bottom-width: 2px;
                        margin-top: 0px;
                        padding-bottom: 0.5ex;
                        color: #eeeeee;
                    }
                    h2 {
                        font-size: 130%;
                        padding: 10px 10px 0.5ex;
                        margin-left: 10px;
                        margin-right: 10px;
                        color: #009ACE;
                        border-bottom-style: solid;
                        border-bottom-width: 2px;
                    }
                    #header {
                        padding: 10px;
                        background-color: #009ACE;
                        color: #eeeeee;
                        border-bottom-style: solid;
                        border-bottom-width: 2px;
                        border-color: #999999;
                    }
                    #data {
                        margin-left: 10px;
                        margin-right: 10px;
                        margin-bottom: 10px;
                    }
                    #data td, th {
                        font-size: 80%;
                        margin-left: 2px;
                        margin-right: 2px;
                        padding: 2px 5px;
                    }
                    #data th {
                        border-bottom-color: #777777;
                        border-bottom-style: solid;
                        border-bottom-width: 2px;
                        background-color: #dddddd;
                        color: #009ACE;
                        vertical-align: top;
                        font-size: 80%;
                        white-space: nowrap;
                    }
                    .odd {
                        background-color: #dddddd;
                    }
                    .even {
                        background-color: #eeeeee;
                    }</style>
            </head>
            <body>
                <div id="header">
                    <h1>Logging table</h1>
                </div>

                <h2>Data</h2>
                <xsl:choose>
                    <xsl:when test="count(logger:row) > 0">
                        <table id="data">
                            <tr>
                                <th rowspan="2">Table</th>
                                <th rowspan="2">User</th>
                                <th rowspan="2">Operation</th>
                                <th rowspan="2">Changed</th>
                                <th colspan="3">Columns</th>
                            </tr>
                            <tr>
                                <th>Column</th>
                                <th>Old</th>
                                <th>New</th>
                            </tr>
                            <xsl:for-each select="logger:row">
                                <xsl:variable name="rows">
                                    <xsl:choose>
                                        <xsl:when
                                            test="count(logger:old_values/logger:col) > count(logger:new_values/logger:col)">
                                            <xsl:value-of
                                                select="count(logger:old_values/logger:col)"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:value-of
                                                select="count(logger:new_values/logger:col)"/>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </xsl:variable>
                                <xsl:variable name="class">
                                    <xsl:choose>
                                        <xsl:when test="position() mod 2 = 0">even</xsl:when>
                                        <xsl:otherwise>odd</xsl:otherwise>
                                    </xsl:choose>
                                </xsl:variable>
                                <tr class="{$class}">
                                    <xsl:attribute name="class">
                                        <xsl:choose>
                                            <xsl:when test="position() mod 2 = 0">even</xsl:when>
                                            <xsl:otherwise>odd</xsl:otherwise>
                                        </xsl:choose>
                                    </xsl:attribute>
                                    <td rowspan="{$rows}">
                                        <xsl:value-of select="logger:table"/>
                                    </td>
                                    <td rowspan="{$rows}">
                                        <xsl:value-of select="logger:user"/>
                                    </td>
                                    <td rowspan="{$rows}">
                                        <xsl:value-of select="logger:operation"/>
                                    </td>
                                    <td rowspan="{$rows}">
                                        <xsl:value-of select="logger:changed"/>
                                    </td>
                                    <td>
                                        <xsl:choose>
                                            <xsl:when test="logger:old_values/logger:col">
                                                <xsl:value-of
                                                  select="logger:old_values/logger:col[1]/@name"/>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                <xsl:value-of
                                                  select="logger:new_values/logger:col[1]/@name"/>
                                            </xsl:otherwise>
                                        </xsl:choose>
                                    </td>
                                    <td>
                                        <xsl:value-of select="logger:old_values/logger:col[1]"/>
                                    </td>
                                    <td>
                                        <xsl:value-of select="logger:new_values/logger:col[1]"/>
                                    </td>
                                    <xsl:choose>
                                        <xsl:when test="logger:old_values/logger:col">
                                            <xsl:for-each
                                                select="logger:old_values/logger:col[position() != 1]">
                                                <xsl:variable name="position"
                                                  select="position() + 1"/>
                                                <tr class="{$class}">
                                                  <td>
                                                  <xsl:value-of select="@name"/>
                                                  </td>
                                                  <td>
                                                  <xsl:choose>
                                                  <xsl:when test="@xsi:nil = 'true'">
                                                  <em>NULL</em>
                                                  </xsl:when>
                                                  <xsl:otherwise>
                                                  <xsl:value-of select="."/>
                                                  </xsl:otherwise>
                                                  </xsl:choose>
                                                  </td>
                                                  <td>
                                                  <xsl:choose>
                                                  <xsl:when
                                                  test="../../logger:new_values/logger:col[position() = $position]/@xsi:nil = 'true'">
                                                  <em>NULL</em>
                                                  </xsl:when>
                                                  <xsl:otherwise>
                                                  <xsl:value-of
                                                  select="../../logger:new_values/logger:col[position() = $position]"
                                                  />
                                                  </xsl:otherwise>
                                                  </xsl:choose>
                                                  </td>
                                                </tr>
                                            </xsl:for-each>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:for-each
                                                select="logger:new_values/logger:col[position() != 1]">
                                                <xsl:variable name="position"
                                                  select="position() + 1"/>
                                                <tr class="{$class}">
                                                  <td>
                                                  <xsl:value-of select="@name"/>
                                                  </td>
                                                  <td>
                                                  <xsl:choose>
                                                  <xsl:when
                                                  test="../../logger:old_values/logger:col[position() = $position]/@xsi:nil = 'true'">
                                                  <em>NULL</em>
                                                  </xsl:when>
                                                  <xsl:otherwise>
                                                  <xsl:value-of
                                                  select="../../logger:old_values/logger:col[position() = $position]"
                                                  />
                                                  </xsl:otherwise>
                                                  </xsl:choose>
                                                  </td>
                                                  <td>
                                                  <xsl:choose>
                                                  <xsl:when test="@xsi:nil = 'true'">
                                                  <em>NULL</em>
                                                  </xsl:when>
                                                  <xsl:otherwise>
                                                  <xsl:value-of select="."/>
                                                  </xsl:otherwise>
                                                  </xsl:choose>
                                                  </td>
                                                </tr>
                                            </xsl:for-each>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </tr>
                            </xsl:for-each>
                        </table>
                    </xsl:when>
                    <xsl:otherwise>
                        <p>no rows returned</p>
                    </xsl:otherwise>
                </xsl:choose>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
